<?php

class Discount_activityModel extends BaseModel
{
    var $table  = 'discount_activity';
    var $prikey = 'discount_id';
    var $_name  = 'discount_activity';
    var $_relation  = array(

    );

    function getGoods($where){
        $goodsModel = &m("goods");
        $goodsSpecModel = &m('goodsspec');
        $my_tax = &m("my_tax");
        $goods_list = $goodsModel->find(array("conditions"=>"goods_id in (".$where.")"));

        foreach ($goods_list as $key => $goods) {
            $goods_list[$key]['cate_name'] = $goodsModel->format_cate_name($goods['cate_name']);
            $goods_list[$key]['default_image'] = WEB_PHOTO_SRC . "/" . $goods['default_image'];
            $taxInfo = $my_tax->get(array("conditions"=>"tax_id =".$goods['tax_id']));
            $goods_list[$key]['consolidated_tax'] = $taxInfo['consolidated_tax'];
            $goods_list[$key]['times'] = $goods['times']?$goods['times']:'无';
            $retult = $goodsSpecModel->find(array('conditions' => "goods_id = " . $key));
            if (!empty($retult)) {
                $str['domestic_price'] = 0;
                $str['domestic_discount'] = '无';
                $str['hong_kong_price'] = 0;
                $str['hong_kong_discount'] = '无';

                //声明一个库存数
                $inventory = 0;
                $in_array = array();
                $stock_array = array();
                foreach ($retult as $k => $v) {
                    $inventory += $v['stock'];
                    if ($v['spec_3value'] != 26) {                    //国内仓标价
                        $str['domestic_price'] = $v['price'];
                        if ($goods['market_price'] && $goods['market_price'] != 0.00) {
                            $str['domestic_discount'] = sprintf('%.2f', $v['price'] / $goods['market_price']) . '折';//国内仓折扣
                        } else {
                            $str['domestic_discount'] = 0 . '折';//国内仓折扣
                        }
                    }
                    if ($v['spec_3value'] == 26) {
                        $str['hong_kong_price'] = $v['price'];
                        if ($v['price'] && $v['price'] != 0.00 && $goods['market_price'] != 0.00) {
                            $str['hong_kong_discount'] = sprintf('%.2f', $v['price'] / $goods['market_price']) . '折';//香港折扣
                        } else {
                            $str['hong_kong_discount'] = 0 . '折';//香港折扣
                        }
                    }
                }
                $res_ss = $goodsSpecModel->getSpecStockInfo($key);
                foreach ($res_ss as $k => $v) {

                    $v['international_code'] && !in_array($v['international_code'], $in_array) ? $in_array[] = $v['international_code'] : '无';
                    $v['stock_name'] && !in_array($v['stock_name'], $stock_array) ? $stock_array[] = $v['stock_name'] : '无';
                    $res_ss[$k]['image'] = WEB_PHOTO_SRC  . "/" . $v['spec_1'];
                    $res_ss[$k]['times'] = $v['times']?$v['times']:'无';
                    $res_ss[$k]['spec_3'] = $v['spec_3']?$v['spec_3']:'无';
                    if($v['stock'] == 0){
                        unset($res_ss[$k]);
                    }
                }
                $in_array ? $goods_list[$key]['international_code'] = implode('<br>', $in_array) : '无';
                $stock_array ? $goods_list[$key]['stock_name'] = implode('<br>', $stock_array) : '无';
                $goods_list[$key]['domestic_price'] = $str['domestic_price'];
                $goods_list[$key]['domestic_discount'] = $str['domestic_discount'];
                $goods_list[$key]['hong_kong_price'] = $str['hong_kong_price'];
                $goods_list[$key]['hong_kong_discount'] = $str['hong_kong_discount'];
                //总库存
                $goods_list[$key]['total_inventory'] = $inventory;

            }
            $goods_list[$key]['specInfo'] = $res_ss;
        }

        return $goods_list;
    }
}

?>
